package com.yisu.kabao.manager.website.controller;

import com.yisu.kabao.manager.core.service.IEditorActionService;
import com.yisu.kabao.manager.domain.request.uploadPackage.UploadImageRequest;
import com.yisu.kabao.manager.domain.response.UploadImageResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/11/25.
 */
@Controller
@RequestMapping(value = "/upload")
public class UploadAction extends HttpServlet {
    @Resource
    private IEditorActionService iEditorActionService;

    //上传文件的保存路径
    protected String configPath = "upload/widget";

    protected String dirTemp = "upload/widget/temp";

    protected String dirName = "file";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadAction() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @RequestMapping(value = "/UploadAction.json")
    @ResponseBody
    protected UploadImageResult doGet(HttpServletRequest request, HttpServletResponse response, UploadImageRequest uploadImageRequest) throws ServletException, IOException {
        // TODO Auto-generated method stub
        UploadImageResult uploadImageResult = doUploadImagePost(request, response);
        return uploadImageResult;
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }

    protected UploadImageResult doUploadImagePost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        UploadImageResult uploadImageResult = iEditorActionService.UploadImage(file);
        return uploadImageResult;
    }




}
